/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package astroLib;

/**
 *
 * @author mehmetrg
 */
public class APC_Vect3D {
  
static class Vec3D {
      double X ,Y,Z;
      boolean mPolarValid=false;
      Vec3D (){
          this.mPolarValid=false;
      }
      Vec3D(Polar polar) {
       double cosEl = Math.cos(polar.theta);
       this.X = polar.r * Math.cos(polar.phi) * cosEl;
       this.Y = polar.r * Math.sin(polar.phi) * cosEl;
       this.Z=  polar.r * Math.sin(polar.theta);
       this.mPolarValid=true;
      }
      Vec3D(double x, double y,double z)
      {this.X=x;this.Y=y;this.Z=z;}

         
    }
static class Polar {
      double phi, theta,r;
      Polar(double phi,double theta)
      {this.phi=phi;this.theta=theta;this.r=1;}
       Polar(double phi,double theta,double r)
      {this.phi=phi;this.theta=theta;this.r=r;}
}


//
// Elementary rotation matrices

 public static double[][] R_x(double RotAngle)
{
  double S = Math.sin (RotAngle);
  double C = Math.cos (RotAngle);

  double MatRx3D[][]=new double[3][3];
  
  MatRx3D[0][0] = 1.0;  MatRx3D[0][1] = 0.0;  MatRx3D[0][2] = 0.0;
  MatRx3D[1][0] = 0.0;  MatRx3D[1][1] =  +C; MatRx3D[1][2] =  +S;
  MatRx3D[2][0] = 0.0;  MatRx3D[2][1] =  -S;  MatRx3D[2][2] =  +C;

  return MatRx3D;
}
static Vec3D AbProduct (double [][] matrix, Vec3D vect)
{
   Vec3D result;
   result=new Vec3D();
   result.X=matrix[0][0]*vect.X+ matrix[0][1]*vect.Y+matrix[0][2]*vect.Z;
   result.Y=matrix[1][0]*vect.X+ matrix[1][1]*vect.Y+matrix[1][2]*vect.Z;
   result.Z=matrix[2][0]*vect.X+ matrix[2][1]*vect.Y+matrix[2][2]*vect.Z;
   return result;
}
 public static double[][] R_y(double RotAngle)
{
  double S = Math.sin (RotAngle);
  double C = Math.cos (RotAngle);

  double MatRy3D[][]=new double[3][3];
  
  MatRy3D[0][0] = +C ;  MatRy3D[0][1] = 0.0;  MatRy3D[0][2] = -S;
  MatRy3D[1][0] = 0.0;  MatRy3D[1][1] =  +1;  MatRy3D[1][2] =0.0;
  MatRy3D[2][0] =  +S;  MatRy3D[2][1] =  0.0; MatRy3D[2][2] = +C;

  return MatRy3D;
}
static  Polar calcPolarAngles (Vec3D vect)
    { 
    Polar polarV = new Polar(0,0);
    double rhoSqr = vect.X * vect.X + vect.Y * vect.Y; 
   // Norm of vector
    polarV.r = Math.sqrt ( rhoSqr + vect.Z * vect.Z );
    // Azimuth of vector
    if ( (vect.X==0.0) && (vect.Y==0.0) ) 
    polarV.phi = 0.0;
    else
    polarV.phi = MATH.atan2 (vect.Y, vect.X);
    if ( polarV.phi < 0.0 )  polarV.phi += 2.0*Math.PI;
    
  // Altitude of vector
    double rho = Math.sqrt ( rhoSqr );
    if ( (vect.Z==0.0) && (rho==0.0) ) 
       polarV.theta = 0.0;
    else
        polarV.theta = MATH.atan2(vect.Z, rho);  
    return polarV;
    }

}
